package org.asdmp.restday.service;

import java.util.List;
import java.util.Set;

import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.IdentityService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ParallelService{
	public static RepositoryService repositoryService;	
	public static ExecutionService executionService;	
	public static TaskService taskService;	
	public static HistoryService historyServcie;	
	public static ManagementService managermentService;	
	public static IdentityService identityService; 	
	public static ProcessEngine processEngine;	
	public static ClassPathXmlApplicationContext applicationContext;	
	public static String processInstanceId;

	public void deploy() {
		startUp();
		repositoryService.createDeployment().addResourceFromClasspath("parallel.jpdl.xml").deploy();
		print("Jerry", "deloy");
	}

	public String createInstance() {
		startUp();
		ProcessInstance processInstance = executionService.startProcessInstanceByKey("parallel");
		processInstanceId = processInstance.getId();
		print("流程实例ID",processInstanceId);
		return processInstanceId;
	}

	public String getCurrectActivity(String processInstanceId) {
		startUp();
		String name = executionService.createProcessInstanceQuery().processInstanceId(processInstanceId).list().get(0).findActiveActivityNames().toString();
		return name;
	}

	public Set<String> getTasks(String processInstanceId){
		startUp();
		Set<String> activityNames = executionService.createProcessInstanceQuery().processInstanceId(processInstanceId).list().get(0).findActiveActivityNames();
		return activityNames;
	}
	
	public String getTask(String processInstanceId){
		startUp();
		String activityName = executionService.createProcessInstanceQuery().processInstanceId(processInstanceId).list().get(0).findActiveActivityNames().toString();
		return activityName;
	}
	
	public void completeTask(String processInstanceId, String subTaskName) {
		startUp();
		ProcessInstance processInstance = executionService.findProcessInstanceById(processInstanceId);
		Set<String> activeActivityNames = processInstance.findActiveActivityNames();
		if (activeActivityNames.size() > 1){
			for (String subpName:activeActivityNames){
				if (subpName.equals(subTaskName)){
					Task task = taskService.createTaskQuery().activityName(subTaskName).uniqueResult();
					taskService.completeTask(task.getId());
				}
			}
		}else if (activeActivityNames.size() == 1){
			Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).uniqueResult();
			taskService.completeTask(task.getId());
		}
	}
	
	
	public Task getSubTask(String nodeName){
		List<Task> tasks = taskService.getSubTasks(processInstanceId);
		for (Task t : tasks){
			if (t.getName().equals(nodeName)) return t;
		}
		return null;
	}
	
	public static void startUp(){
		applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		applicationContext.start();
		processEngine = (ProcessEngine)applicationContext.getBean("processEngine");
		repositoryService = processEngine.getRepositoryService();
		executionService = processEngine.getExecutionService();
		taskService = processEngine.getTaskService();
		historyServcie = processEngine.getHistoryService();
		managermentService = processEngine.getManagementService();
		identityService = processEngine.getIdentityService();
	}
	
	public List<ProcessInstance> listAllProcessInstances(){
		startUp();
		return (List<ProcessInstance>)executionService.createProcessInstanceQuery().list();
	}
	
	public void print(String name,String value){
		System.out.println(name + "=============" + value);
	}
	
	public void print(String name,int value){
		System.out.println(name + "=============" + value);
	}
}
